/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

package org.openmrs.module.datalogging.filter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.context.Context;
import org.openmrs.module.datalogging.enums.FilterType;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class FilterFactory {

	private static final Log log = LogFactory.getLog(FilterFactory.class);

	private static FilterFactory instance;

	private Map<String, Filter> filterHandlerMapping;

	private FilterFactory() {
		registerDefaultHandler();
	}

	private void registerDefaultHandler() {
		Filter conceptFilter = new Filter("Concept Filter", Context.getConceptService());
		conceptFilter.addTypeHandler(FilterType.PARTIAL_NAME, "getConceptsByName");
		conceptFilter.addTypeHandler(FilterType.OBJECT_ID, "getConcept");
		conceptFilter.addTypeHandler(FilterType.FULL_NAME, "getConceptByName");
		getFilterHandlerMapping().put("concept", conceptFilter);

		Filter codedFilter = new Filter("Value Coded Filter", Context.getConceptService());
		codedFilter.addTypeHandler(FilterType.PARTIAL_NAME, "getConceptsByName");
		codedFilter.addTypeHandler(FilterType.OBJECT_ID, "getConcept");
		codedFilter.addTypeHandler(FilterType.FULL_NAME, "getConceptByName");
		getFilterHandlerMapping().put("valueCoded", codedFilter);

		Filter locationFilter = new Filter("Location Filter", Context.getLocationService());
		locationFilter.addTypeHandler(FilterType.PARTIAL_NAME, "getLocations");
		locationFilter.addTypeHandler(FilterType.OBJECT_ID, "getLocation");
		locationFilter.addTypeHandler(FilterType.FULL_NAME, "getLocation");
		getFilterHandlerMapping().put("location", locationFilter);

		Filter encounterTypeFilter = new Filter("Encounter Type Filter", Context.getEncounterService());
		encounterTypeFilter.addTypeHandler(FilterType.PARTIAL_NAME, "findEncounterTypes");
		encounterTypeFilter.addTypeHandler(FilterType.OBJECT_ID, "getEncounterType");
		encounterTypeFilter.addTypeHandler(FilterType.FULL_NAME, "getEncounterType");
		getFilterHandlerMapping().put("encounterType", encounterTypeFilter);
	}

	/**
	 * @return
	 */
	private synchronized static FilterFactory getInstance() {
		if (instance == null)
			instance = new FilterFactory();
		return instance;
	}

	/**
	 * Return the value of the filterHandlerMapping
	 *
	 * @return the filterHandlerMapping
	 */
	private Map<String, Filter> getFilterHandlerMapping() {
		if (filterHandlerMapping == null)
			filterHandlerMapping = new HashMap<String, Filter>();
		return filterHandlerMapping;
	}

	/**
	 * Register a new handler to create Rule object
	 *
	 * @param filter  the filter name
	 * @param handler the handler class
	 */
	private static void registerHandler(String filter, Filter handler) {
		getInstance().getFilterHandlerMapping().put(filter, handler);
	}

	/**
	 * Search for a filter handler in all registered handler
	 *
	 * @param filter the filter name
	 * @return handler of the filter or null if no handler is registered for the filter
	 */
	public static Filter getHandler(String filter) {
		return getInstance().getFilterHandlerMapping().get(filter);
	}

	/**
	 * Un-register a filter handler
	 *
	 * @param filter the filter that will be unregistered
	 */
	public static void unregisterHandler(String filter) {
		getInstance().getFilterHandlerMapping().remove(filter);
	}

}
